package com.felix.io.nio;

/**
 * Client
 *
 * @author xjh
 * @version 1.0
 * @date 2019-02-21
 */
public class Client {
    private static String DEFAULT_HOST = "127.0.0.1";
    private static int DEFAULT_PORT = 12345;
    private static ClientHandler clientHandler;

    public static void start() {
        start(DEFAULT_HOST, DEFAULT_PORT);
    }

    public static synchronized void start(String ip, int port) {
        if (clientHandler != null) {
            clientHandler.stop();
        }
        clientHandler = new ClientHandler(ip, port);
        new Thread(clientHandler, "Server").start();
    }

    /**
     * 向服务器发送消息
     */
    public static boolean sendMsg(String msg) throws Exception {
        if ("q".equals(msg)) {
            return false;
        }
        clientHandler.sendMsg(msg);
        return true;
    }

    public static void main(String[] args) {
        start();
    }
}
